import Reflux from 'reflux';
import PageConst from './PageConst';
import JDJLConst from './../../home/txhome/PageConst';
import Actions from './Actions';
import { Control } from 'react-keeper'
import update from 'react-addons-update';
import StorageUtils from '../../../app/storage';
import Utils from './../../../utils';
import moment from 'moment';
import DB from './../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,...JDJLConst,
    txmap : new Map(),
    //滚动组件
    infs: null,
    //查询到的日志
    rzls: [],
    //选中的日志
    currentObj: {},
    //选中工程
    currentEng: {},

    //搜索关键字
    keyword: '',
    //当前页数
    currentPage: 1,
    pageSize: 5,
    isUpload: false,
    lastPage: false,
    isStart: false,
    //本弹窗是否显示
    fullpage_show: false,
    jlrq:{},
    jlicon:{},
    jlpdf:'',
    jllx:'',
    router:'',
    //工程id
    engid:'',
    //条件搜索
    searchText:'',
    items: [],
    userInfo: {
      certNum: '',
      telNum: '',
      vcode: '',
      name:'',
      organName:''
    },
  },



  onSetCurrentObj: function (params) {
    let t = this;
    this.data.currentObj = update(this.data.currentObj,  {$set: params});
    let lx = this.data.currentObj.jllx;
    let merObj = this.data.txmap.get(lx);

    this.data.currentObj = update(this.data.currentObj,  {$merge: merObj});




    console.info('合并666666666666666666');
    console.info(this.data.currentObj);
    this.updateComponent();
  },


  onHandleCancel: function () {
    Actions.initMode();
    Actions.loadMode('0008_0022');
    this.updateComponent();
  },

  onGoInfo: function (obj) {

  },

  onKeySearch: function (params) {
    let t = this;
    params=params.replace(/(^\s+)|(\s+$)/g, "")
    this.data.lastPage = update(this.data.lastPage, {$set: true});
    this.data.currentPage = update(this.data.currentPage, {$set: 1});
    this.data.currentObj = update(this.data.currentObj, {$set: {}});
    this.data.jlicon = update(this.data.jlicon, {$set: {}});
    this.data.rzls = update(this.data.rzls, {$set: []});
    this.data.items = update(this.data.items, {$set: []});


    this.data.keyword = update(this.data.keyword, {$set: params});
    this.data.isStart = update(this.data.isStart, {$set: true});



    if(this.data.isStart)
    {
      if(this.data.keyword.length>0)
      {
        console.info('测试isstart 进入'+this.data.isStart);
        this.onKeyFetch({
          page: 1,
          tel:'13972608122',
          jllx:'8888',
          engName:'',
          pageSize: 5,
          rq:'',
        });
      }
    }
    t.updateComponent();
  },



  onLoadZG: function (model) {
    let t = this;

    this.onZGFetch({
      page: 1,
      tel:'13972608122',
      jllx:'8888',
      engName:'',
      pageSize: 5,
    });
  },

  onZGFetch: function (params) {
    let t = this;

    params.jllx = this.data.jllx;
    params.engName = this.data.keyword;
    params.page = this.data.currentPage;
    params.pageSize = this.data.pageSize;
    console.info("质量安全整改未回复。。。。。。。onZGFetch");
    if (!__LOCAL__) {
      params.tel = this.data.userInfo.telNum;
    }
    console.info(params);
    DB.WechatAPI.findJLByLxMany(params).then(
      function (content) {
        console.info(content);
        t.data.items = update(t.data.items, {$set: content.resultls});
        console.info(t.data.items);
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });


  },

  initMode: function () {
    this.data.jllx = update(this.data.jllx, {$set: ''});
    this.data.keyword = update(this.data.keyword, {$set: ''});
    this.data.currentPage = update(this.data.currentPage, {$set: 1});
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
    this.data.items = update(this.data.items, {$set: []});

    this.data.txmap.set("0008", this.data.zlzg);
    this.data.txmap.set("0022", this.data.aqzg);
    this.data.txmap.set("0009", this.data.zltg);
    this.data.txmap.set("0023", this.data.aqtg);
    this.data.txmap.set("0052", this.data.aqjb);

    this.updateComponent();
  },


  loadMode: function (params) {
    let t = this;
    this.data.jllx = update(this.data.jllx,   {$set: params});
    this.data.isStart = update(this.data.isStart, {$set: true});
    Actions.loadZG(true);
    this.updateComponent();
  },



  onAddPage(resolve,finish){

    this.data.rzls = update(this.data.rzls, {$set: []});

    this.data.currentPage = update(this.data.currentPage,  {$set: this.data.currentPage+1});
    this.onJlFetch({
      page: 1,
      pageSize: 5,
      tel:'13972608122',
      jllx:'8888',
      engName:'',
      rq:'',
    },resolve,finish);
/*    if(count=== this.data.items.length)
    {
      console.info("质量安全整改未回复。。。。。。。finish");
      finish();
    }
    else
    {
      console.info("质量安全整改未回复。。。。。。。resolve");
      resolve();
    }*/
  },


  onJlFetch: function (params,resolve,finish) {
    console.info('onJlFetch');
    let t = this;
    params.page = t.data.currentPage;
    params.pageSize = t.data.pageSize;
    params.engName = t.data.keyword;
    params.jllx = t.data.jllx;
    params.engid = t.data.engid;
    console.info("质量安全整改未回复。。。。。。。onJlFetch");
    console.info(params);
    let resultRq = Utils.isEmptyObject(this.data.jlrq);
    console.info(resultRq.success);
    if (!resultRq.success)
    {
      params.rq = '';
    }

      if (!t.data.lastPage)
      {}

      /*t.data.lastPage = update(t.data.lastPage, {$set: true});*/
        DB.WechatAPI.findJLByLxMany(params).then(
         function (content) {
            if (content != null || content.resultls != null)  {
              t.data.rzls = update(t.data.rzls, {$set: content.resultls});
              t.data.items = update(t.data.items, {$push: t.data.rzls});
              if (t.data.rzls.length < t.data.pageSize) {
                finish();
              }
              else
              {
                resolve();
              }
              console.info('循环查询');
              console.info(t.data.rzls);
            }
/*                       if (t.data.rzls.length < 5) {
                         t.data.lastPage = update(t.data.lastPage, {$set: true});
                       }
                       else {
                         t.data.lastPage = update(t.data.lastPage, {$set: false});
                       }   */
           t.data.rzls = update(t.data.rzls, {$set: []});


        },
        function (error) {
          console.log("错误");
          t.data.lastPage = update(t.data.lastPage, {$set: true});
          console.log(error);
        });
        t.updateComponent();

  },

  onKeyFetch: function (params) {
    console.info('onKeyFetch');
    let t = this;
    params.page = t.data.currentPage;
    params.pageSize = 10;
    params.engName = t.data.keyword;
    params.jllx = t.data.jllx;
    params.engid = t.data.engid;
    let resultRq = Utils.isEmptyObject(this.data.jlrq);
    console.info(resultRq.success);
    if (!resultRq.success)
    {
      params.rq = '';
      console.info('日期空');
      console.info(this.data.jlrq);
    }
    else
    {
      console.info('日期有');
    }

    console.info('参数');
    console.info(params);

    DB.WechatAPI.findJLByLxMany(params).then(
      function (content) {
        if (content != null) {
          console.info('日期有进入',content.resultls);
          t.data.items = update(t.data.items, {$set: content.resultls});
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },



  onJlrqChange: function (params) {
    console.log('action_jlrqChange');
    this.data.lastPage = update(this.data.lastPage, {$set: true});
    this.data.jlrq = update(this.data.jlrq,  {$set: moment(params)});
    this.onKeyFetch({
      page: 1,
      size: 50,
      engName:'',
      engid:'',
      userid:'',
      rq:params,
    });
    this.updateComponent();
  },

  onjlrqClear: function (params) {
    console.info('onjlrqClear');
    this.data.jlrq = update(this.data.jlrq,  {$set: {}});
    this.onJlrqChange(this.data.jlrq);
    console.info('日期');
    console.info(this.data.jlrq);
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'1397260812cc2',
      rq:'',
    });
    this.updateComponent();
  },






  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }


});
